#!/usr/bin/env python
# -*- indent-tabs-mode: nil; tab-width: 4; coding: utf-8 -*-
# vi: set ts=4 sts=4 sw=4 set smarttab set expandtab
# http://www.careercup.com/question?id=15139669
"""
Write a method that takes a camilCase string as a parameter and returns underscore_case as output. Assume that input can be null or empty. If CamilCase parametar starts with a capital letter turn it into lower case without puting underscore before it. How do you test this method?
"""

import os, re, sys, getopt
import logging
import locale
from utils import *
import copy

@time_profile
def camil_to_underscore_david(s):
    if not s: return s
    result = []
    Z = ord('Z')
    s = [ord(c) for c in s]
    result = [s[0] <= Z and chr(s[0] + 32) or chr(s[0])]
    for i in range(1, len(s)):
        if s[i] <= Z:
            result.append('_')
            result.append(chr(s[i] + 32))
        else:
            result.append(chr(s[i]))
    return "".join(result)

@time_profile
def camil_to_underscore_lobatt(s):
    if not s or len(s) <= 0: return s
    result = "";
    ord_a = ord('a')
    ord_A = ord('A')
    for i in range(0, len(s)):
        ch = ord(s[i])
        if ch < ord_A: result += s[i]
        elif ch < ord_a:
            if i != 0: result += '_'
            result += chr(ch + 32)
        else:
            result += s[i]
    return result


if __name__ == '__main__':
    for i in range(0, 10):
        s = random_str(150)
        david = camil_to_underscore_david(s)
        lobatt = camil_to_underscore_lobatt(s)
        if david == lobatt:
            print_pass()
        else:
            print_fail()
            print s
            print david
            print lobatt
